﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using Newtonsoft.Json;
using System.Windows.Forms;
using System.IO;
using System.Runtime.ConstrainedExecution;

namespace oled
{
    class Json : oled_panelbuff
    {
        public string name = null;
    }
    class oled_panelbuff
    {
        public int Pos_X = 0;
        public int Pos_Y = 0;
        public int Height = 0;
        public int Width = 0;
        public int size = 0;
        public string string_data = null;
        public byte[] buff = null;
    }
    class Json_unpck
    {
        public int unpackTo_oled(string Json_str)
        {
            OLED_Drive oled = new OLED_Drive();
            Json Json_pack = JsonConvert.DeserializeObject<Json>(Json_str);
            int ret = 0;
            if (Json_pack.name == "string")
            {
                oled.ShowString(
                    Json_pack.Pos_X,
                    Json_pack.Pos_Y,
                    Json_pack.string_data,
                    Json_pack.size);
                ret = 1;
            }
            else if (Json_pack.name == "bmp")
            {
                string filepath = AppDomain.CurrentDomain.BaseDirectory;
                filepath = filepath + "program.json";
                StreamReader reader = new StreamReader(filepath);
                Json file_buff;

                string file_str = reader.ReadToEnd();

                file_buff = JsonConvert.DeserializeObject<Json>(file_str);

                oled.ShowBMP(
                    file_buff.Pos_X,
                    file_buff.Pos_Y,
                    file_buff.Width,
                    file_buff.Height,
                    file_buff.buff);
                ret = 2;
                reader.Close();
            }
            else if (Json_pack.name == "Clear")
            {
                oled.Clear();
                oled.Windows_Clear(
                    Json_pack.Pos_X,
                    Json_pack.Pos_Y,
                    Json_pack.Width,
                    Json_pack.Height);
                ret = 3;
            }
            return ret;
        }
    }
}
